package com.stars.empoly.vmodel

import android.app.Application
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.stars.empoly.activity.PlaceOrderActivity
import com.stars.empoly.componnent.CpAlipay
import com.stars.empoly.fragment.AccountBillFragment
import com.stars.empoly.model.AmountCount
import com.stars.empoly.model.OrderList
import com.stars.empoly.model.User
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

/**
 *
 *on 2020/9/18
 */
class HomeListVModel(application: Application) : BRViewModel<BRRepository>(application) {


    var status:String?="-1"

    var type=MutableLiveData<String>()
    var id:String?=null
    var money:String?=null
    var payType:Int?=0


    fun toPay(id:String?,payMoney:String?,payType:Int?){
        this.id=id
        this.payType=payType
        money=payMoney
        type.value="1"
    }
    fun isShowComent(isComment:Int?,status:Int):Int{

        if(status==3&&isComment==0){
            return View.VISIBLE
        }
        return View.GONE
    }

    //再来一单
    fun toAgain(bean: OrderList?){

        UiSwitch.bundle(repository().context, PlaceOrderActivity::class.java,
            B().putString("id","-2")
                .putString("beanId", bean?.id).ok())

//        UiSwitch.bundle(repository().context, PlaceOrderActivity::class.java,
//            B().putString("id","-1")
//            .putString("bean", Gson().toJson(bean)).ok())

    }

    fun toPay(type:String?){


        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.payOrder(id,type) }
            if(it.ok()){

                if(type == "1"){
                   toast("支付成功")
                    firstReq()
                }
                if(type == "2"){
                    //微信支付

                }
                if(type == "3"){
                    val orderInfo = it.data?.alipay
                    CpAlipay.pay(repository().context!!, orderInfo!!)
                }

            }
        }

    }


    override fun apiFunc(): suspend () -> IResp<*> {


        return { NetEngine.service.orderList(status,10,repository().page) }

    }
}
